<?php

namespace App\Http\Controllers;

use App\Models\Admin;
use App\Models\Basic;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redis;

class BasicController extends AdminController
{


    /**
     * 显示页面
     */
    public function index(Request $request)
    {
        if ($request->getMethod() == "POST") {
            $formData = $request->all();
            $add = $request->add;
            $basic = new  Basic();
            foreach ($formData as $key => $value) {
                if ($key != 'add') {
                    $val = ['value' => $value];
                    $basic->where("id", $key)->update($val);
                }
            }
            if (!empty($add)) {//增加
                $basic->insert($add);
            }
            return redirect("/basic")->with("messages", ['0' => "修改成功"]);
        } else {
            /**
             * 查询数据
             */
            $basic = Basic::where('act','1')->get();
            return view("basic.index", ['basic' => $basic]);
        }
    }

    /**
     * @param Request $request
     * 添加公共配置
     */

    public function set(Request $request){

        $percent = $request->percent;
        $desc= $request->percent.'%配置';
        $number = $request->number;

        $basic = new Basic();
        $basic->percent=$percent;
        $basic->value=$number;
        $basic->desc=$desc;
        if($basic->save()){
            $this->setlog(json_encode($basic),'添加升级配置项');
            return redirect("/basic")->with("messages", ['0' => "配置添加成功"]);
        }else{
            return redirect("/basic")
                ->withErrors(array("添加失败"))
                ->withInput();
        }
    }

    public function delete($id){
        $post = Basic::find($id);
        if($post->delete()){
            $this->setlog(json_encode($post),'删除'.$post->percent.'配置项');
            return redirect("/basic")->with("messages", ['0' => "删除成功"]);
        }else{
            return redirect("/basic")
                ->withErrors(array("删除失败"))
                ->withInput();
        }
    }
}
